﻿<script type="text/javascript">
    var mmenufavorites = {
        dialogInit: function () {
            var d = manage.dialog({
                title: "收藏夹编辑",
                width: 732, height: 388,
                href: "/manage/menufavorites/page",
                onLoad: function () { mmenufavorites.gridInit(); },
                onClose: function () { manage.menuFavoritesInit(); }
            });
        },
        gridInit: function () {
            $("#mmenufavorites-menugrid").treegrid({
                title: "全部菜单（双击添加）",
                url: "/manage/menu/usergrid",
                nowrap: false,
                rownumbers: true,
                border: false,
                fit: true,
                singleSelect: true,
                idField: "ID",
                treeField: "Name",
                parentField: "Pid",
                columns:
                [[
                    { field: "Name", title: "名称", width: 200, align: "left" },
                    { field: "Type", title: "类型", width: 80, align: "center" },
                    { field: "Module", title: "模块", width: 100, align: "center" },
                    {
                        field: "IsEnabled", title: "启用", width: 50, align: "center",
                        formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                    }
                ]],
                onDblClickRow: mmenufavorites.added
            });
            $("#mmenufavorites-grid").datagrid({
                url: "/manage/menufavorites/grid",
                loadMsg: "Loading...",
                title: "已选菜单（双击删除）",
                nowrap: false,
                rownumbers: true,
                idField: "ID",
                fit: true,
                border: false,
                singleSelect: true,
                toolbar: "#mmenufavorites-tools",
                columns: [[
                    {
                        field: "Icon", title: "图标", width: 50, align: "center",
                        formatter: function (value, row, index) {
                            if (row.BasicMenu) {
                                return manage.icon(row.BasicMenu.Icon);
                            }
                        }
                    },
                    {
                        field: "Name", title: "名称", width: 140, align: "center",
                        formatter: function (value, row, index) {
                            if (row.BasicMenu) {
                                return row.BasicMenu.Name;
                            }
                        }
                    }
                ]],
                onDblClickRow: mmenufavorites.remove
            });
        },
        added: function (row) {
            if (row) {
                if (row.Type == "null") {
                    manage.msg("不能收藏类型为 null 的菜单"); return false;
                }
                if (row.Type == "embed") {
                    manage.msg("不能收藏类型为 embed 的菜单"); return false;
                }
                var rows = $("#mmenufavorites-grid").datagrid("getRows");
                for (var i = 0; i < rows.length; i++) {
                    if (row.ID == rows[i].MenuID) { return false; }
                }
                manage.ajax({
                    url: "/manage/menufavorites/insert",
                    type: "post", data: { menuid: row.ID },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            mmenufavorites.reload();
                        }
                    }
                });
            }
        },
        remove: function (index, row) {
            if (row) {
                manage.ajax({
                    url: "/manage/menufavorites/remove",
                    type: "post", data: { id: row.ID },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            mmenufavorites.reload();
                        }
                    }
                });
            }
        },
        clear: function () {
            manage.confirm("确认清除所有收藏?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/menufavorites/clear",
                        type: "get",
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) {
                                mmenufavorites.reload();
                            }
                        }
                    });
                }
            });
        },
        order: function (order) {
            var row = $("#mmenufavorites-grid").datagrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/manage/menufavorites/setorder",
                    type: "post", data: { id: row.ID, order: order },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#mmenufavorites-grid").datagrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        },
        reload: function () {
            $("#mmenufavorites-grid").datagrid("unselectAll");
            $("#mmenufavorites-grid").datagrid("reload");
        }
    };

    $(function () { mmenufavorites.dialogInit(); });
</script>